import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {Http,Jsonp} from "@angular/http";

import { NewscontentPage} from "../newscontent/newscontent";

@IonicPage()
@Component({
  selector: 'page-uprefresh',
  templateUrl: 'uprefresh.html',
})
export class UprefreshPage {
  public list=[];
  public page=1;
  public NewscontentPage = NewscontentPage;

  constructor(public navCtrl: NavController, public navParams: NavParams,public http:Http,public jsonp:Jsonp) {
    this.requestData('');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad UprefreshPage');
  }
  requestData(infiniteScroll){
    var that = this;
    var url='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page='+this.page;
      this.http.get(url).subscribe(function(data){
          // console.log(data['_body']);

          var response=JSON.parse(data['_body']).result;
          that.list=that.list.concat(response);   /*拼接每次请求的数据*/
          console.log(that.list);
          that.page++;
          /*每次请求完成page+1 */

          if(infiniteScroll){
              infiniteScroll.complete();   /*请求完成数据更新页面*/
              if(that.page>3){   /*没有数据 禁用上拉更新*/
                  infiniteScroll.enable(false);
              }
          }
          console.log('上拉加载成功' + that.page);

      },function(err){

      })
  }
    /*上拉加载*/
    doInfinite(infiniteScroll){  /*接收事件对象传值*/
        // console.log('11111')
        this.requestData(infiniteScroll);

    }
    /*下拉刷新新*/
    doRefresh(refresher){
        this.page = 1;
        this.list = [];
        this.requestData('');
        refresher.complete();  /*加载完成以后重新渲染页面*/
        console.log('下拉刷新成功');
    }
}
